package com.github.kuangcp.mock;

import com.github.kuangcp.mock.common.MockHelper;
import com.github.kuangcp.mock.common.MockUsuallyValue;
import java.util.HashSet;
import java.util.Objects;
import java.util.Set;
import lombok.extern.slf4j.Slf4j;

/**
 * @author https://github.com/kuangcp on 2019-07-14 15:44
 */
@Slf4j
public class MockSet {

  public static <T> Set<T> mock(long size, T bound) {
    HashSet<T> result = new HashSet<>((int) size);
    mock(result, size, bound);
    return result;
  }

  public static <T> Set<T> mock(long size, Class<T> type) {
    HashSet<T> result = new HashSet<>((int) size);
    mock(result, size, MockUsuallyValue.mock(type));
    return result;
  }

  public static <T> void mock(Set<T> set, long size, T bound) {
    if (Objects.isNull(set)) {
      return;
    }
    if (!MockUsuallyValue.isSupportType(bound.getClass())) {
      log.error("not support type: key={}", bound.getClass().getName());
      return;
    }

    try {
      size = MockHelper.min(bound, size);
      while (set.size() < size) {
        set.add(MockUsuallyValue.mock(bound));
      }
    } catch (Exception e) {
      log.error(e.getMessage(), e);
    }
  }

  public static <T> void mock(Set<T> set, long size, Class<T> type) {
    mock(set, size, MockUsuallyValue.mock(type));
  }
}
